﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Trakt.Model;

namespace Trakt
{
    internal class TraktResponseConverter : JsonConverter
    {
        private static readonly Type[] _EmptyTypes = new Type[] { };

        public override bool CanConvert(Type objectType)
        {
            return typeof(IJsonDeserializable).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var result = (IJsonDeserializable)objectType.GetConstructor(_EmptyTypes).Invoke(_EmptyTypes);
            result.Deserialize(reader, serializer);
            return result;
        }
        
        public override bool CanWrite { get { return false; } }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new InvalidOperationException("Response objects can only be read.");
        }
    }
}
